﻿
namespace adsf
{
    using System;
    using System.Windows.Forms;
    using Facebook;
    using System.Diagnostics;

    public partial class MainForm : Form
    {
        private const string AppId = "407490639285023";

        /// <summary>
        /// Extended permissions is a comma separated list of permissions to ask the user.
        /// </summary>
        /// <remarks>
        /// For extensive list of available extended permissions refer to 
        /// https://developers.facebook.com/docs/reference/api/permissions/
        /// </remarks>
        private const string ExtendedPermissions = "user_about_me,publish_stream";

        public MainForm()
        {
            InitializeComponent();
            progressBar1.Visible = false;
            login(false);
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            login();
        }

        private void login(bool visible = true)
        {
            // open the Facebook Login Dialog and ask for user permissions.
            var fbLoginDlg = new FacebookLoginDialog(AppId, ExtendedPermissions);

            if (visible)
            {
                fbLoginDlg.ShowDialog();
                progressBar1.Visible = true;
                progressBar1.Value = progressBar1.Maximum;
                TakeLoggedInAction(fbLoginDlg.FacebookOAuthResult);
            }
            else
            {
                fbLoginDlg.login();
                progressBar1.Visible = true;
                progressBar1.Value = progressBar1.Maximum;
                btnLogin.Enabled = false;
                UseWaitCursor = true;
                int count = 0;
                Timer t = new Timer();
                t.Tick += (o, a) => {
                progressBar1.Visible = true;
                progressBar1.Value = progressBar1.Maximum;
                    Timer s = o as Timer;

                    if (fbLoginDlg.FacebookOAuthResult != null)
                    {
                        s.Stop();
                        TakeLoggedInAction(fbLoginDlg.FacebookOAuthResult);
                    }

                    if (++count > 30)
                    {
                        s.Stop();
                        progressBar1.Visible = false;
                        progressBar1.Value = progressBar1.Maximum;
                        btnLogin.Enabled = true;
                        UseWaitCursor = false;
                        return;
                    }
                    
                };
                t.Interval = 60;
                t.Start();
            }
        }

        private void TakeLoggedInAction(FacebookOAuthResult facebookOAuthResult)
        {
            if (facebookOAuthResult == null)
            {
                // the user closed the FacebookLoginDialog, so do nothing.
                MessageBox.Show("Cancelled!");
                return;
            }

            // Even though facebookOAuthResult is not null, it could had been an 
            // OAuth 2.0 error, so make sure to check IsSuccess property always.
            if (facebookOAuthResult.IsSuccess)
            {
                // since our respone_type in FacebookLoginDialog was token,
                // we got the access_token
                // The user now has successfully granted permission to our app.
                var dlg = new InfoDialog(facebookOAuthResult.AccessToken);
                dlg.ShowDialog();
            }
            else
            {
                // for some reason we failed to get the access token.
                // most likely the user clicked don't allow.
                MessageBox.Show(facebookOAuthResult.ErrorDescription);
            }
        }
    }
}
